#****************************************************************************
#
# Makefile for distrrtgen 3.
# 
#  alesc <alexis.dagues@gmail.com>
#
# This is a GNU make (gmake) makefile
#****************************************************************************

# DEBUG can be set to YES to include debugging info, or NO otherwise
DEBUG          := NO 

# PROFILE can be set to YES to include profiling info, or NO otherwise
PROFILE        := NO

# TINYXML_USE_STL can be used to turn on STL support. NO, then STL
# will not be used. YES will include the STL files.
TINYXML_USE_STL := YES 

#****************************************************************************

CC     := gcc
CXX    := g++
LD     := g++
AR     := ar rc
RANLIB := ranlib

PREFIX?  := /usr/local/bin
TMPLCONF := distrrtgen.conf
CONFDIR  := ~/.distrrtgen
CONFFILE := $(CONFDIR)/distrrtgen.conf

DEBUG_CFLAGS     :=  -D_FAST_HASH_ -D_FILE_OFFSET_BITS=64 -Wall -Wno-format -g -DDEBUG
RELEASE_CFLAGS   :=  -D_FAST_MD5_ -D_FAST_HASH_ -D_FILE_OFFSET_BITS=64 -Wno-unknown-pragmas -Wno-format -march=x86-64 -O2

LIBS		 := -lssl -lpthread -lcurl -lgsoap

DEBUG_CXXFLAGS   := ${DEBUG_CFLAGS} 
RELEASE_CXXFLAGS := ${RELEASE_CFLAGS}

DEBUG_LDFLAGS    := -g
RELEASE_LDFLAGS  := 

ifeq (YES, ${DEBUG})
   CFLAGS       := ${DEBUG_CFLAGS}
   CXXFLAGS     := ${DEBUG_CXXFLAGS}
   LDFLAGS      := ${DEBUG_LDFLAGS}
else
   CFLAGS       := ${RELEASE_CFLAGS}
   CXXFLAGS     := ${RELEASE_CXXFLAGS}
   LDFLAGS      := ${RELEASE_LDFLAGS}
endif

ifeq (YES, ${PROFILE})
   CFLAGS   := ${CFLAGS} -pg -O3
   CXXFLAGS := ${CXXFLAGS} -pg -O3
   LDFLAGS  := ${LDFLAGS} -pg
endif

#****************************************************************************
# Preprocessor directives
#****************************************************************************

ifeq (YES, ${TINYXML_USE_STL})
  DEFS := -DTIXML_USE_STL
else
  DEFS :=
endif

#****************************************************************************
# Include paths
#****************************************************************************

#INCS := -I/usr/include/g++-2 -I/usr/local/include
INCS :=


#****************************************************************************
# Makefile code common to all platforms
#****************************************************************************

CFLAGS   := ${CFLAGS}   ${DEFS}
CXXFLAGS := ${CXXFLAGS} ${DEFS}

#****************************************************************************
# Targets of the build
#****************************************************************************

OUTPUT := distrrtgen3 

all: ${OUTPUT}


#****************************************************************************
# Source files
#****************************************************************************

SRCS := soapC.c soapClient.c stdsoap2.cpp tinyxml.cpp tinyxmlparser.cpp tinyxmlerror.cpp tinystr.cpp Public.cpp ChainWalkContext.cpp HashAlgorithm.cpp HashRoutine.cpp rtgen_client.cpp BaseSocket.cpp ClientSocket.cpp Exception.cpp SocketException.cpp ServerConnector.cpp Thread.cpp DataGenerationThread.cpp RainbowTableGenerator.cpp MD5new.cpp WU_mgr.cpp

# Add on the sources for libraries
SRCS := ${SRCS}

OBJS := $(addsuffix .o,$(basename ${SRCS}))

#****************************************************************************
# Output
#****************************************************************************

${OUTPUT}: ${OBJS}
	${LD} -o $@ ${LDFLAGS} ${OBJS} ${LIBS} ${EXTRA_LIBS}

#****************************************************************************
# common rules
#****************************************************************************

# Rules for compiling source files to object files
%.o : %.cpp
	${CXX} -c ${CXXFLAGS} ${INCS} $< -o $@

%.o : %.c
	${CC} -c ${CFLAGS} ${INCS} $< -o $@

dist:
	bash makedistlinux

install: dist configure
		mkdir -p $(PREFIX)
		cp distrrtgen $(PREFIX)

configure:
		@if [ `uname` = "FreeBSD" -a ! -e $(CONFFILE) ]; then   \
				mkdir -p $(CONFDIR) ;                           \
				sed "s|NUMCPU=.*$$|NUMCPU=`sysctl -n hw.ncpu`|" $(TMPLCONF) > $(CONFFILE); \
				chmod 600 $(CONFFILE);                          \
		fi
		@if [ `uname` = "Linux" -a ! -e $(CONFFILE) ]; then  \
				mkdir -p $(CONFDIR) ;   \
				sed "s|NUMCPU=.*$$|NUMCPU=`grep -c ^processor /proc/cpuinfo`|" $(TMPLCONF) > $(CONFFILE); \
				chmod 600 $(CONFFILE);	\
		fi
		@if [ ! -e $(CONFFILE) ]; then       \
			mkdir -p $(CONFDIR) ;        \
			cp $(TMPLCONF) $(CONFFILE) ; \
			chmod 600 $(CONFFILE);       \
		fi

clean:
	-rm -f core ${OBJS} ${OUTPUT}

depend:
	#makedepend ${INCS} ${SRCS}

tinyxml.o: tinyxml.h tinystr.h
tinyxmlparser.o: tinyxml.h tinystr.h
tinyxmlerror.o: tinyxml.h tinystr.h
